﻿namespace ufida.u9.ait.openapi
{
	using System;
	using System.Collections.Generic;
	using System.Text;
    using ufida.u9.ait.openapi.utils;
    using UFIDA.U9.Base;
    using UFIDA.U9.CBO.Pub.Controller;
    using UFIDA.U9.CBO.SCM.ProjectTask;
    using UFSoft.UBF.AopFrame;	
	using UFSoft.UBF.Util.Context;

	/// <summary>
	/// ProjectApproveSV partial 
	/// </summary>	
	public partial class ProjectApproveSV 
	{	
		internal BaseStrategy Select()
		{
			return new ProjectApproveSVImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class ProjectApproveSVImpementStrategy : BaseStrategy
	{
		public ProjectApproveSVImpementStrategy() { }

		public override object Do(object obj)
		{						
			ProjectApproveSV bpObj = (ProjectApproveSV)obj;

            try
            {
                if (bpObj.Data == null || bpObj.Data.Count == 0)
                    throw new Exception("传入参数为空");
                List<CommonArchiveDataDTOData> retlist = new List<CommonArchiveDataDTOData>();
                foreach (var dto in bpObj.Data)
                {
                    Project entity = Project.Finder.Find($"Code='{dto.Code}' and Org={Context.LoginOrg.ID}");
                    if (entity == null)
                    {
                        throw new Exception($"查找物料失败,物料编码:[{dto.Code}],组织编码:[{Context.LoginOrg.Code}]");
                    }
                    if(entity.State==ProjectStateEnum.Aproved)
                        throw new Exception($"编码[{entity.Code}]状态为已审核");

                    ProjectApproveBP bp = new ProjectApproveBP();
                    bp.ProjectID = entity.ID;
                    bp.Do();

                    retlist.Add(new CommonArchiveDataDTOData
                    {
                        ID = entity.ID,
                        Code = entity.Code,
                        Name = entity.Name,
                    });

                }

                return ResultUtils.success(bpObj, bpObj.Data, retlist);
            }
            catch (Exception ex)
            {
                return ResultUtils.error(bpObj, bpObj.Data, ex);
            }
        }		
	}

	#endregion
	
	
}